<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/6 0006
 * Time: 17:01
 */

namespace app\home\controller;


use app\model\model\Free_tags;
use app\model\model\Nav;
use app\model\model\Product_type;
use app\model\model\Seo;
use app\model\model\Setup;
use app\model\model\Partners;
use app\model\model\Social;
use app\model\model\Solution_type;
use think\Controller;
use think\Request;

class Base extends Controller
{
    protected $_Freetags;
    protected $_Partners;
    protected $_Setup;
    protected $_Social;
    protected $_Nav;
    protected $_Seo;
    protected $_product;
    protected $_producttype;
    protected $_solution;
    public function _initialize()
    {

        $this->_Freetags = new Free_tags();
        $this->_Setup = new Setup();
        $this->_Social = new Social();
        $this->_Nav = new Nav();
        $this->_Seo = new Seo();
        $this->_Partners = new Partners();
        $this->_producttype= new Product_type();
        $this->_product = new \app\model\model\Product();
        $this->_solution = new Solution_type();
        //导航
        $nav = $this->_Nav->getListHome(['type'=>1,'flag'=>1]);
        $navlist = [];
        foreach ($nav as $k=>$v){
            $navlist[$k] = [
                'id'=>$v['id'],
                'name'=>$v['name'],
                'url'=>$v['url'],
            ];
            if($v['id']==23){
                $solution = $this->_solution->getListSelect();
                foreach ($solution as $kk=>$vv){
                    if($vv['id']!=16){
                        $name = str_replace(" ","-",$vv['name']);
                        $navlist[$k]['data'][$kk]=[
                            'id'=>$vv['id'],
                            'name'=>$vv['name'],
                            'url'=>'/Solution-Category/'.$vv['id'].'.html',
                        ];
                    }

                }
            }
            if($v['id']==22){
                $ptype = $this->_producttype->getListSelect(true,['rank'=>0]);

                foreach ($ptype as $key=>$value){
                    if($value['id']!=12){
                        $name = str_replace(" ","-",$value['name']);
                        $navlist[$k]['data'][$key]=[
                            'id'=>$value['id'],
                            'name'=>$value['name'],
                            'url'=>'/Product-Category/'.$value['id'].'.html',
                        ];
                        $ptyperank = $this->_product->where(['type_id'=>$value['id'],'flag'=>1])->order('sort_nav asc')->select();

                        foreach ($ptyperank as $kkk=>$vvv){
                            $name1 = str_replace(" ","-",$vvv['name']);
                            $navlist[$k]['data'][$key]['rank'][$kkk]=[
                                'id'=>$vvv['id'],
                                'name'=>$vvv['name'],
                                'url'=>'/product/'.$vvv['id'].'.html',
                            ];
                        }
                    }

                }
            }else{
                $dataTwo = $this->_Nav->getListHome(['type'=>$v['id'],'flag'=>1]);
                foreach ($dataTwo as $key=>$value){
                    $navlist[$k]['data'][$key]=[
                        'id'=>$value['id'],
                        'name'=>$value['name'],
                        'url'=>$value['url'],
                    ];
                }
            }

        }
        $this->assign('nav',$navlist);
        //合作伙伴
        $partners = $this->_Partners->getListHome([],6);
        $this->assign('partners',$partners);
        //底部产品分类
        $protype = $this->_producttype->getListSelect(true,['rank'=>0]);
        foreach ($protype as $key=>$value){
            $value['uname'] = str_replace(" ","-",$value['name']);
            $navlist[$k]['data'][$key]=[
                'id'=>$value['id'],
                'name'=>$value['name'],
                'url'=>'/Product-Category/'.$name.'-'.$value['id'].'.html',
            ];
        }
        $this->assign('protype',$protype);
        //SEO
        $seo = $this->_Seo->getOne(['id'=>1]);
        $this->assign('seo',$seo);
        //底部信息
        $CONTACT_US = $this->_Freetags->getOne(22);
        $this->assign('CONTACT_US',$CONTACT_US);
        //基本信息
        $setup = $this->_Setup->getOne(['id'=>1]);
        $this->assign('setup',$setup);
        //社交信息
        $social = $this->_Social->getOne(1);
        $this->assign('social',$social);

    }
    protected function _accessPv(){
        $access_model = new  \app\model\model\Accesspv();

        $request = Request::instance();

        $params = [
            'user_id'       => session('admin_id'),
            'page'          => $request->url(true),
            'agent'         => $request->header('user-agent'),
            'ip'            => $request->ip(),
            'city'          => taobaoIP($request->ip()),
            'browser'       => getBrowser($request->header('user-agent')),
            'create_time'   => time(),
        ];
        $access_model->add($params);

        $access_model = new  \app\model\model\Accessip();
        $data = $access_model->getDetail(['ip'=>$params['ip'],'create_time'=>['EGT', strtotime(date('Y-m-d'))]]);
        if(empty($data)){
            $params = [
                'user_id'       => session('admin_id'),
                'agent'         => $request->header('user-agent'),
                'ip'            => $request->ip(),
                'city'          => taobaoIP($request->ip()),
                'browser'       => getBrowser($request->header('user-agent')),
                'create_time'   => time(),
            ];
            $access_model->add($params);
        }
    }
}